﻿using Common.Attributes;
using MODEL;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Zmjh
{
    public class GraduManageController :Controller
    {
        public ActionResult index()
        {
            return View();
        }

        #region 获取竹铭课程报名学生数据列表 + GetTrainerData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取竹铭课程报名学生数据列表
        /// </summary>
        /// <retur
        public ActionResult GetTrainerData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);

            var list = OperateContext.Current.BLLSession.Iqgzx_zm_trainerBLL.GetPagedList(pageIndex, pageSize, x => x.TrainId != null, m => m.TrainId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_zm_trainerBLL.GetListBy(x => x.TrainId != null).Count();

            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改竹铭课程 window 的html + editGraduManage()
        /// <summary>
        /// 加载修改竹铭期数开启 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult editGraduManage(int id)
        {
            SetDropDonwList();
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_zm_trainerBLL.GetListBy(x => x.TrainId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改课程信息 editGraduManage(MODEL.qgzx_zm_trainer model)
        /// <summary>
        ///post修改期数信息 editGraduManage(MODEL.qgzx_zm_trainer model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult editGraduManage(MODEL.qgzx_zm_trainer model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_zm_trainerBLL.Modify(model, "TrainId", "TrainerNum", "PeriodId", "StudentNum", "AttendanceScore", "TestScore", "FeelingScore", "VolunteerName", "VolunTime", "IsGraduated", "IsExcellent", "TotalScore");
            if (res > 0)
            {
                return Redirect("/Zmjh/GraduManage/index");
            }
            return Redirect("/Zmjh/GraduManage/index");
        }
        #endregion

        #region 删除课程 + DelStu()
        /// <summary>
        /// 删除课程 + DelCourse()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelStu()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_zm_trainerBLL.DelBy(x => x.TrainId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion

        #region 在这新增课程 html代码 + AddStu()
        /// <summary>
        /// 在这新增新一期竹铭并且开启报名系统  html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddStu()
        {

            //SetDropDonwList();
            return PartialView("EditGraduManage");
        }
        #endregion

        #region 在这新增课程 html代码 + AddStu(MODEL.qgzx_zm_trainer model)
        /// <summary>
        /// 在这新增期数 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddStu(MODEL.qgzx_zm_trainer model)
        {
            //qgzx_zm_period mode = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetListBy(x => x.IsOpen == 1).FirstOrDefault();
            //model.PeriodId = mode.Period;
            //model.IsChosed = 0;
            //model.ApplyTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_zm_trainerBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/Zmjh/GraduManage/index?ok");
            }
            return Redirect("/Zmjh/GraduManage/index?err");
        }
        #endregion


        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string StudentNum = Request.QueryString["StudentNum"];
            string PeriodId = Request.QueryString["PeriodId"];
            string CourseName = Request.QueryString["CourseName"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_zm_trainerBLL.GetListBy(x => x.TrainId != null).ToList();
            if (PeriodId != "")
            {
                list = list.Where(x => x.PeriodId.ToString().Contains(PeriodId)).ToList();
            }
            if (StudentNum != "")
            {
                list = list.Where(x => x.StudentNum.ToString().Contains(StudentNum)).ToList();
            }

            //if (CourseName != "")
            //{
            //    list = list.Where(x => x.CourseName.ToString().Contains(CourseName)).ToList();
            //}

            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion


        #region 设置新增和修改通用的下拉框数据 + SetDropDonwList()
        /// <summary>
        /// 设置新增和修改通用的下拉框数据
        /// </summary>
        void SetDropDonwList()
        {
            /*
             课程id对应课程
             */
            List<MODEL.qgzx_zm_course> list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.Id != null);
            List<SelectListItem> selectList = new List<SelectListItem>();
            foreach (MODEL.qgzx_zm_course model in list)
            {
                selectList.Add(new SelectListItem() { Text = model.CourseName, Value = model.Id.ToString() });
            }
            ViewBag.courselist = selectList;


        }
        #endregion
   
    
    
    
    
    
    
    
    }







}
